/**
 * 车辆配置问题 - 编辑页面
 * @author miaoyl
 * @date 2018/01/14
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            form: {
                GWCA011: '',
                GWCA012: '',
                GWCA013: '',
                GWCA014: '',
                GWCA015: '',
                GWCA016: '',
                GWCA011_NAME: '',
                GWCA012_NAME: '',
                GWCA013_NAME: '',
                GWCA014_NAME: '',
                GWCA015_NAME: '',
                DEVICEADDRESS: ''
            },
            rules: {

            },
            selectOptions: {
                gwca011Options: [],
                gwca012Options: [],
                gwca013Options: [],
                gwca014Options: [],
                gwca015Options: []
            },
        },
        pageVue,
        callObj, layOpenIndex,
        cb
    ;

    var Gwci001Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwci001_edit_address',
                _loadTplsFull: [/*{
                    dom: 'common_open_window', tpl: '/common/_open_window'
                }*/]
            }, win);
        },

        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            layOpenIndex = obj.layOpenIndex;
            callObj = obj;
            cb = obj.cb;
            app.extendUpLow({
                target: vueData.form,
                res: [callObj.data,{
                    GWCA011_NAME: '',
                    GWCA012_NAME: '',
                    GWCA013_NAME: '',
                    GWCA014_NAME: '',
                    GWCA015_NAME: ''
                }],
                reset: true
            });
            let gwca011Data = {};
            gwca011Data = vueData.selectOptions.gwca011Options.find(function (item) {
                return item.GWAD002 === vueData.form.GWCA011
            });
            vueData.form.GWCA011_NAME = gwca011Data != undefined ? gwca011Data.GWADL004 : '';
            if(vueData.form.GWCA011 != '' && vueData.form.GWCA011 != null){
                self.getAddress();
            }
        },

        getAddress:function () {
            vueData.selectOptions.gwca012Options = [];
            app.ajax({
                xurl: 'crm/gwai300/getGwadList',
                xdata: {gwad001: '5', gwad003: vueData.form.GWCA011, type: '4'},
                cb: function (result) {
                    vueData.selectOptions.gwca012Options.pushArray(result.data.data);
                    var obj = {};
                    obj = vueData.selectOptions.gwca012Options.find(function (item) {
                        return item.GWAD002 === vueData.form.GWCA012
                    });
                    vueData.form.GWCA012_NAME = obj != undefined ? obj.GWADL004 : '';
                }
            });
            vueData.selectOptions.gwca013Options = [];
            if(vueData.form.GWCA012 != '' && vueData.form.GWCA012 != null) {
                app.ajax({
                    xurl: 'crm/gwai300/getGwadList',
                    xdata: {gwad001: '6', gwad003: vueData.form.GWCA012, type: '5'},
                    cb: function (result) {
                        vueData.selectOptions.gwca013Options.pushArray(result.data.data);
                        var obj = {};
                        obj = vueData.selectOptions.gwca013Options.find(function (item) {
                            return item.GWAD002 === vueData.form.GWCA013
                        });
                        vueData.form.GWCA013_NAME = obj != undefined ? obj.GWADL004 : '';
                    }
                });
            }
            vueData.selectOptions.gwca014Options = [];
            if(vueData.form.GWCA013 != '' && vueData.form.GWCA013 != null) {
                app.ajax({
                    xurl: 'crm/gwai300/getGwadList',
                    xdata: {gwad001: '7', gwad003: vueData.form.GWCA013, type: '6'},
                    cb: function (result) {
                        vueData.selectOptions.gwca014Options.pushArray(result.data.data);
                        var obj = {};
                        obj = vueData.selectOptions.gwca014Options.find(function (item) {
                            return item.GWAD002 === vueData.form.GWCA014
                        });
                        vueData.form.GWCA014_NAME = obj != undefined ? obj.GWADL004 : '';
                    }
                });
            }
            vueData.selectOptions.gwca015Options = [];
            if(vueData.form.GWCA014 != '' && vueData.form.GWCA014 != null) {
                app.ajax({
                    xurl: 'crm/gwai300/getGwadList',
                    xdata: {gwad001: '8', gwad003: vueData.form.GWCA014, type: '7'},
                    cb: function (result) {
                        vueData.selectOptions.gwca015Options.pushArray(result.data.data);
                        var obj = {};
                        obj = vueData.selectOptions.gwca015Options.find(function (item) {
                            return item.GWAD002 === vueData.form.GWCA015
                        });
                        vueData.form.GWCA015_NAME = obj != undefined ? obj.GWADL004 : '';
                    }
                });
            }
        },

        _ready: function () {
            pageVue = new Vue({
                el: '#gwci001_edit_address',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                vueData.form.DEVICEADDRESS = vueData.form.GWCA011_NAME + vueData.form.GWCA012_NAME + vueData.form.GWCA013_NAME
                                +vueData.form.GWCA014_NAME+vueData.form.GWCA015_NAME + (vueData.form.GWCA016 == undefined ? '': vueData.form.GWCA016);
                                cb(vueData.form);
                                layer.close(layOpenIndex);
                            }
                        });
                    },
                    changeGwca011: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.gwca011Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.GWCA011_NAME = obj.GWADL004;
                        pageVue.form.GWCA012 = "";
                        pageVue.form.GWCA012_NAME = "";
                        pageVue.form.GWCA013 = "";
                        pageVue.form.GWCA013_NAME = "";
                        pageVue.form.GWCA014 = "";
                        pageVue.form.GWCA014_NAME = "";
                        pageVue.form.GWCA015 = "";
                        pageVue.form.GWCA015_NAME = "";
                        pageVue.form.GWCA016 = "";
                        vueData.selectOptions.gwca012Options = [];
                        app.ajax({
                            xurl: 'crm/gwai300/getGwadList',
                            xdata: {gwad001: '5', gwad003: vueData.form.GWCA011, type: '4'},
                            cb: function (result) {
                                vueData.selectOptions.gwca012Options.pushArray(result.data.data);
                            }
                        });
                    },
                    changeGwca012: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.gwca012Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.GWCA012_NAME = obj.GWADL004;
                        pageVue.form.GWCA013 = "";
                        pageVue.form.GWCA013_NAME = "";
                        pageVue.form.GWCA014 = "";
                        pageVue.form.GWCA014_NAME = "";
                        pageVue.form.GWCA015 = "";
                        pageVue.form.GWCA015_NAME = "";
                        pageVue.form.GWCA016 = "";
                        vueData.selectOptions.gwca013Options = [];
                        app.ajax({
                            xurl: 'crm/gwai300/getGwadList',
                            xdata: {gwad001: '6', gwad003: vueData.form.GWCA012, type: '5'},
                            cb: function (result) {
                                vueData.selectOptions.gwca013Options.pushArray(result.data.data);
                            }
                        });
                    },
                    changeGwca013: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.gwca013Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.GWCA013_NAME = obj.GWADL004;
                        pageVue.form.GWCA014 = "";
                        pageVue.form.GWCA014_NAME = "";
                        pageVue.form.GWCA015 = "";
                        pageVue.form.GWCA015_NAME = "";
                        pageVue.form.GWCA016 = "";
                        vueData.selectOptions.gwca014Options = [];
                        app.ajax({
                            xurl: 'crm/gwai300/getGwadList',
                            xdata: {gwad001: '7', gwad003: vueData.form.GWCA013, type: '6'},
                            cb: function (result) {
                                vueData.selectOptions.gwca014Options.pushArray(result.data.data);
                            }
                        });
                    },
                    changeGwca014: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.gwca014Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.GWCA014_NAME = obj.GWADL004;
                        pageVue.form.GWCA015 = "";
                        pageVue.form.GWCA015_NAME = "";
                        pageVue.form.GWCA016 = "";
                        vueData.selectOptions.gwca015Options = [];
                        app.ajax({
                            xurl: 'crm/gwai300/getGwadList',
                            xdata: {gwad001: '8', gwad003: vueData.form.GWCA014, type: '7'},
                            cb: function (result) {
                                vueData.selectOptions.gwca015Options.pushArray(result.data.data);
                            }
                        });
                    },
                    changeGwca015: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.gwca015Options.find(function (item) {
                            return item.GWAD002 === val
                        });
                        vueData.form.GWCA015_NAME = obj.GWADL004;
                        pageVue.form.GWCA016 = "";
                    }
                },
                created: function () {
                    self._setEleIsReady('gwci001_edit_address');
                },
                watch: {

                }
            });
        },

        _lazy: function () {
            this._super();

            app.ajax({
                xurl: 'crm/gwai300/getGwadList',
                xdata: {gwad001: '4', type: '3'},
                cb: function (result) {
                    vueData.selectOptions.gwca011Options.pushArray(result.data.data);
                }
            });
        }
    });

    return new Gwci001Edit();

});